package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;

/**
 * 导表工具测试
 */
public class PoiDemo {

    /**
     * Poi写数据测试
     *
     * @throws IOException
     */
    @Test
    public void TestWriteByPoi() throws IOException {
        //创建Excel文件对象
        Workbook wb = new XSSFWorkbook();
        //创建工作表对象
        Sheet sheet = wb.createSheet();
        wb.createSheet("我是个表");

        //创建工作表行对象
        Row row = sheet.createRow(1);
        //创建工作表列对象
        Cell cell = row.createCell(1);
        //设置数据
        cell.setCellValue("这是一条测试数据");

        //创建输出流对象
        OutputStream os = new FileOutputStream(new File("test.xlsx"));
        //写出
        wb.write(os);

        //释放资源
        wb.close();
        os.close();
    }

    /**
     * Poi读表测试
     */
    @Test
    public void TestReadByPoi() throws IOException {
        //获取文件工作簿对象
        Workbook wb = new XSSFWorkbook("test.xlsx");
        //获取工作表对象
        Sheet sheet = wb.getSheetAt(0);
        //获取行
        Row row = sheet.getRow(1);
        //获取列
        Cell cell = row.getCell(1);
        //通过单元格获取值
//        boolean value = cell.getBooleanCellValue();   //获取其他类型 详见方法
        String value = cell.getStringCellValue();
        //打印
        System.out.println(value);

        wb.close();

    }



}
